﻿using MediatR;
using W.Purchase.Domain;
using W.Purchase.Read.API.AppLication.Command.FinanceTypeCommand;
using W.Purchase.Repository.Interface;

namespace W.Purchase.Read.API.AppLication.CommandHandler.FinanceTypeCommandHandler
{
    public class FinanceTypeShowCommandHandler:IRequestHandler<FinanceTypeShowCommand,List<FinanceType>>
    {
        private readonly IFinanceTypesRepository _ifinanceTypesRepository;
        public FinanceTypeShowCommandHandler(IFinanceTypesRepository ifinanceTypesRepository)
        {
            _ifinanceTypesRepository = ifinanceTypesRepository;
        }

        public async Task<List<FinanceType>> Handle(FinanceTypeShowCommand request, CancellationToken cancellationToken)
        {
            var list = await _ifinanceTypesRepository.GetAllAsaync();
            var plist = list.Where(c => c.IsDelete == false).ToList();
            if (!string.IsNullOrEmpty(request.FinanceTypeName))
            {
                plist=plist.Where(c=>c.FinanceTypeName==request.FinanceTypeName).ToList();
            }
            return plist.OrderByDescending(c=>c.FinanceTypeId).ToList();
        }
    }
}
